package main

import (
	"fmt"

	"github.com/spf13/viper"
)

type Host struct {
	Address string
	Port    int
}

type Config struct {
	AppId  string
	Secret string
	Host   Host
}

func main() {
	config := viper.New()
	config.AddConfigPath(".")
	config.SetConfigName("app.json")
	config.SetConfigType("json")

	if err := config.ReadInConfig(); err != nil {
		fmt.Println(err)
	}

	fmt.Println(config.GetString("appId"))
	fmt.Println(config.GetString("host.port"))

	//反序列化
	var configJson Config

	if err := config.Unmarshal(&configJson); err != nil {
		fmt.Println(err)
	}

	fmt.Println(configJson.Host.Address)
	fmt.Println(configJson.AppId)
}
